package com.ms.p2p.template.directive;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.common.utils.DirectiveUtils;
import com.ms.p2p.bean.PageBean;
import com.ms.p2p.entity.content.Article;
import com.ms.p2p.provider.ArticleProvider;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import org.springframework.stereotype.Repository;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by xing_ on 2017/5/21.
 */
@Repository
public class ArticleDirective extends BaseDirective {

    @Reference
    private ArticleProvider articleProvider;

    public static final String CATE_ID = "cateId";

    @Override
    public void execute(Environment env, Map params, TemplateModel[] templateModels,
                        TemplateDirectiveBody body) throws TemplateException, IOException {
        Integer cateId = DirectiveUtils.getInt(CATE_ID, params);
        Integer count = getCount(params);
        PageBean page = new PageBean();
        page.setPageSize(count);
        List<Article> list = articleProvider.getIndexNotice(cateId, page);
        renderParamToBody("newsList", list, env, body);
    }
}
